/**
 * Copyright &copy; 2016-2017 <a href="https://www.lczyfz.com">联创智云</a> All rights reserved.
 */
package com.lczyfz.campusinfo.modules.campusinfoexam.web;

import com.lczyfz.campusinfo.common.enums.CampusinfoOrderStat;
import com.lczyfz.campusinfo.common.enums.OrderType;
import com.lczyfz.campusinfo.common.enums.PayType;
import com.lczyfz.campusinfo.common.persistence.Page;
import com.lczyfz.campusinfo.common.utils.ApiResultUtils;
import com.lczyfz.campusinfo.common.web.BaseController;
import com.lczyfz.campusinfo.modules.campusinfoexam.entity.CampusinfoExam;
import com.lczyfz.campusinfo.modules.campusinfoexam.entity.CampusinfoExamName;
import com.lczyfz.campusinfo.modules.campusinfoexam.entity.CampusinfoExamType;
import com.lczyfz.campusinfo.modules.campusinfoexam.service.CampusinfoExamNameService;
import com.lczyfz.campusinfo.modules.campusinfoexam.service.CampusinfoExamService;;
import com.lczyfz.campusinfo.modules.campusinfoexam.service.CampusinfoExamTypeService;
import com.lczyfz.campusinfo.modules.campusinfoorder.service.CampusinfoOrderService;
import com.lczyfz.campusinfo.modules.campusinfopay.service.CampusinfoPayService;
import com.lczyfz.campusinfo.modules.sys.utils.UserUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.Map;

/**
 * 模拟考试业务Controller
 * @author zms
 * @version 2018-04-13
 */
@Controller
@RequestMapping(value = "api/campusinfoeducation/campusinfoExam")
public class CampusinfoApiExamController extends BaseController {

	@Autowired
	private CampusinfoExamService campusinfoExamService;
	@Autowired
	private CampusinfoExamNameService campusinfoExamNameService;
	@Autowired
	private CampusinfoOrderService campusinfoOrderService;
	@Autowired
	private CampusinfoExamTypeService campusinfoExamTypeService;
	@Autowired
	private CampusinfoPayService campusinfoPayService;
	@RequestMapping(value = "businessList")
	@ResponseBody
	public Map<String, Object> list(CampusinfoExamType campusinfoExamType, HttpServletRequest request, HttpServletResponse response, Model model) {
		Map<String, Object> map = ApiResultUtils.getBaseMapSuccess();
		Page<CampusinfoExamType> page = campusinfoExamTypeService.findPage(new Page<CampusinfoExamType>(request, response), campusinfoExamType);
		map.put("page", page.getList());
		map.put("pageNo", page.getPageNo());
		map.put("count", page.getCount());
		map.put("totalPage", page.getTotalPage());
		return map;
	}
	@RequestMapping(value = "examList")  //模拟考试列表
	@ResponseBody
	public Map<String, Object> getexams(CampusinfoExamName campusinfoExamName, HttpServletRequest request, HttpServletResponse response, Model model) {
		Map<String, Object> map = ApiResultUtils.getBaseMapSuccess();
		Page<CampusinfoExamName> page = campusinfoExamNameService.findPage(new Page<CampusinfoExamName>(request, response), campusinfoExamName);
		map.put("page", page.getList());
		map.put("pageNo", page.getPageNo());
		map.put("count", page.getCount());
		map.put("totalPage", page.getTotalPage());
		return map;
	}
	@RequestMapping(value = "findById")//模拟考试详情
	@ResponseBody
	public Map<String, Object> getexam(CampusinfoExamName campusinfoExamName, HttpServletRequest request, HttpServletResponse response, Model model) {
		Map<String, Object> map = ApiResultUtils.getBaseMapSuccess();
		CampusinfoExamName page = campusinfoExamNameService.get(campusinfoExamName);
		map.put("page", page);
		return map;
	}
	@RequestMapping(value = "enlist")  //模拟考试报名
	@ResponseBody
	public Map<String, Object> getenlist(CampusinfoExam campusinfoExam, HttpServletRequest request, HttpServletResponse response, Model model) {
		Map<String, Object> map = ApiResultUtils.getBaseMapSuccess();
		if (campusinfoExam != null) {
			campusinfoExam.setUser(UserUtils.getUser());
			CampusinfoExam findcampusinfoExam  =campusinfoExamService.getByName(campusinfoExam);
			if(findcampusinfoExam==null) {
				campusinfoExam.setOrderCode(campusinfoOrderService.saveOrderCode(OrderType.Exam.getValue())); //生成订单号
				campusinfoExamService.save(campusinfoExam);
				CampusinfoExamName campusinfoExamName =campusinfoExamNameService.get(campusinfoExam.getExamId());
				String goodName =  campusinfoExamName.getExamName()+ OrderType.Exam.getDesc();
				campusinfoPayService.ordersaveinsert(campusinfoExam.getOrderCode(),campusinfoExam.getPaymentMethod(),goodName, campusinfoExamName.getPayment()); //保存订单号
				map.put("message", "报名成功");
			} else {
				findcampusinfoExam.setPaymentMethod(PayType.UNline.getValue());
				campusinfoExamService.save(findcampusinfoExam);
				map.put("message","用户已经报名");
			}
		} else {
			map.put("message", "报名失败");
		}
		return map;
	}
	@RequestMapping(value = "alipayAddexam")  //模拟考试线下支付
	@ResponseBody
	public Map<String, Object> alipayAddexam(CampusinfoExam campusinfoExam, HttpServletRequest request, HttpServletResponse response, Model model) {
		Map<String, Object> map = ApiResultUtils.getBaseMapSuccess();
		if (campusinfoExam != null) {
			campusinfoExam.setUser(UserUtils.getUser());
			CampusinfoExam findcampusinfoExam  =campusinfoExamService.getByName(campusinfoExam);
			if(findcampusinfoExam ==null) {
				campusinfoExam.setOrderCode(campusinfoOrderService.saveOrderCode(OrderType.Exam.getValue())); //生成订单号
				campusinfoExamService.save(campusinfoExam);
				CampusinfoExamName campusinfoExamName =campusinfoExamNameService.get(campusinfoExam.getExamId());
				String goodName =  campusinfoExamName.getExamName()+ OrderType.Exam.getDesc();
				campusinfoPayService.ordersaveinsert(campusinfoExam.getOrderCode(),campusinfoExam.getPaymentMethod(),goodName,campusinfoExamName.getPayment()); //保存订单号
				map.put("message", "报名成功");
				map.put("campusinfoExam", campusinfoExam);
			} else if(findcampusinfoExam.getStatus().equals(CampusinfoOrderStat.UNDONE.getValue())){
				map.put("campusinfoExam", findcampusinfoExam);
				map.put("message","用户已经报名但是未支付");
			} else {
				map.put("message","用户已经支付完成");
				map.put("success",false);
			}
		} else {
			map.put("message", "报名失败");
		}
		return map;
	}

}